//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ArticleMapper;
import com.itheima.pojo.Article;
import com.itheima.pojo.PageBean;
import com.itheima.service.ArticleService;
import com.itheima.utils.ThreadLocalUtil;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    public ArticleServiceImpl() {
    }

    public void add(Article article) {
        Map<String, Object> map = (Map)ThreadLocalUtil.get();
        Integer userId = (Integer)map.get("id");
        article.setCreateUser(userId);
        this.articleMapper.add(article);
    }

    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        PageBean<Article> pb = new PageBean();
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> map = (Map)ThreadLocalUtil.get();
        Integer userId = (Integer)map.get("id");
        List<Article> as = this.articleMapper.list(userId, categoryId, state);
        Page<Article> p = (Page)as;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    public Article findById(Integer id) {
        Article a = this.articleMapper.findById(id);
        return a;
    }

    public void update(Article article) {
        this.articleMapper.update(article);
    }

    public void deleteById(Integer id) {
        this.articleMapper.deleteById(id);
    }
}
